home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / freeware / openvip.exe / {app} / openvip.py < prev    next >
Encoding:
Python Source  |  2003-05-25  |  7.3 KB  |  224 lines

  1. #
  2. # This file is part of OpenVIP (http://openvip.sourceforge.net)
  3. #
  4. # Copyright (C) 2002-2003
  5. # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
  6. #
  7. # This program is licensed under GNU General Public License version 2;
  8. # see file COPYING in the top level directory for details.
  9. #
  10. # $Id: openvip.py,v 1.20 2003/05/25 09:28:36 vaclavslavik Exp $
  11. #
  12. # Highlevel Python interface to OpenVIP
  13. #
  14.  
  15. import openvip_c
  16. openvip_c.InitExtension()
  17.  
  18. VERSION = openvip_c.OPENVIP_VERSION
  19.  
  20. PLUGIN_VIDEO_FILTER = openvip_c.OPENVIP_PLUGIN_VIDEO_FILTER
  21. PLUGIN_AUDIO_FILTER = openvip_c.OPENVIP_PLUGIN_AUDIO_FILTER
  22. PLUGIN_VIDEO_TRANSITION = openvip_c.OPENVIP_PLUGIN_VIDEO_TRANSITION
  23. PLUGIN_AUDIO_TRANSITION = openvip_c.OPENVIP_PLUGIN_AUDIO_TRANSITION
  24. PLUGIN_INPUT = openvip_c.OPENVIP_PLUGIN_INPUT
  25. PLUGIN_OUTPUT = openvip_c.OPENVIP_PLUGIN_OUTPUT
  26.  
  27.  
  28. class Error(Exception):
  29.     def __init__(self, msg):
  30.         self.msg = msg
  31.     def __str__(self):
  32.         return self.msg
  33.  
  34.  
  35. class UICallback:
  36.     def show_gauge(self): pass
  37.     def update_gauge(self, value): return 1
  38.     def hide_gauge(self): pass
  39.     def set_gauge_text(self, msg): pass
  40.     def log_info(self, msg): pass
  41.     def log_error(self, msg): pass
  42.     def log_warning(self, msg): pass
  43.  
  44. NullUICallback = UICallback()
  45.  
  46.  
  47. class PluginInfo:
  48.     def __init__(self, obj):
  49.         self.name = obj.name        
  50.         self.description = obj.description
  51.  
  52. class VideoStreamInfo:
  53.     def __init__(self, i):
  54.         self.name = i.name
  55.         self.width = i.width
  56.         self.height = i.height
  57.         self.fps = i.fps
  58.         self.aspect = i.aspect
  59.         self.length = i.length
  60.  
  61. class AudioStreamInfo:
  62.     def __init__(self, i):
  63.         self.name = i.name
  64.         self.sample_rate = i.sample_rate
  65.         self.length = i.length
  66.         self.channels = i.channels
  67.  
  68. class FileInfo:
  69.     def __init__(self, fi_c):
  70.         self.filename = fi_c.filename
  71.         self.video_streams = []
  72.         self.audio_streams = []
  73.         for i in range(0, fi_c.video_cnt):
  74.             x = openvip_c.openvip_video_stream_info_t_array_getitem(
  75.                     fi_c.video_streams, i)
  76.             self.video_streams.append(VideoStreamInfo(x))
  77.         for i in range(0, fi_c.audio_cnt):
  78.             x = openvip_c.openvip_audio_stream_info_t_array_getitem(
  79.                     fi_c.audio_streams, i)
  80.             self.audio_streams.append(AudioStreamInfo(x))
  81.  
  82.     def get_stream(self, name):
  83.         """Return object describing given stream or None if not present."""
  84.         for s in self.video_streams:
  85.             if s.name == name: return s
  86.         for s in self.audio_streams:
  87.             if s.name == name: return s
  88.         return None
  89.  
  90.  
  91. class DestCallback:
  92.     def __init__(self, callback_func):
  93.         self.func = callback_func
  94.     def set_dim(self, w, h):
  95.         self.width = w
  96.         self.height = h
  97.     def add(self, data):
  98.         self.func(data, self.width, self.height)
  99.  
  100. class DestCallbackPIL(DestCallback):
  101.     """Callback object that converts data to Python Imaging Library object."""
  102.     def __init__(self, callback_func):
  103.         import Image
  104.         self.func = callback_func
  105.     def add(self, data):
  106.         import Image
  107.         img = Image.fromstring("RGB", (self.width, self.height), data)
  108.         self.func(img)
  109.  
  110. class Task:
  111.     def __init__(self, lib, task, dest=None):
  112.         self.lib = lib
  113.         self.ctask = task
  114.         self.dest = dest
  115.     def __del__(self):
  116.         openvip_c.openvip_destroy_task(self.lib.clib, self.ctask)
  117.  
  118.     def render(self, callback=None):
  119.         if openvip_c.openvipRender(self.lib.clib, self.ctask, callback) == 0:
  120.             raise Error('rendering failed')
  121.  
  122.     def render_single_frame(self, frame):
  123.         return openvip_c.RenderSingleFrame(self.lib.clib, self.ctask,
  124.                                            frame,
  125.                                            self.width*self.height*3)
  126.  
  127.  
  128. class Library:
  129.     def __init__(self, path = None):
  130.         if path == None:
  131.             self.clib = openvip_c.openvip_create_with_defaults()
  132.         else:
  133.             self.clib = openvip_c.openvip_create(path)
  134.     def __del__(self):
  135.         openvip_c.openvip_destroy(self.clib)
  136.  
  137.     def __make_dest_t(self, width, height, dest):
  138.         d = openvip_c.openvip_dest_t()
  139.         if isinstance(dest, DestCallback):
  140.             dest.set_dim(width, height)
  141.             openvip_c.SetDestCallback(d, dest)
  142.         else:
  143.             raise TypeError('dest must derive from openvip.DestFile or openvip.DestCallback')
  144.         return d
  145.  
  146.  
  147.     def load_network_file(self, filename):
  148.         t = openvip_c.openvip_load_network_file(self.clib, filename)
  149.         if t == None:
  150.             raise Error("failed to load file '%s'" % filename)
  151.         return Task(self, t)
  152.  
  153.     
  154.     def load_network_from_string(self, xmldata):
  155.         t = openvip_c.openvip_load_network_from_string(self.clib, xmldata)
  156.         if t == None:
  157.             raise Error("failed to load XML data:\n%s" % xmldata)
  158.         return Task(self, t)
  159.  
  160.  
  161.     def load_network_with_memoutput_from_string(self, xmldata, outputid,
  162.                                                 width, height, dest):
  163.         d = self.__make_dest_t(width, height, dest)
  164.         t = openvip_c.openvip_load_network_with_memoutput_from_string(self.clib, xmldata, outputid, d)
  165.         if t == None:
  166.             raise Error("failed to load XML data:\n%s" % xmldata)
  167.         task = Task(self, t, dest)
  168.         task.width = width
  169.         task.height = height
  170.         return task
  171.  
  172.     def create_thumbnails_generator(self,file,stream,width,height,frames,dest):
  173.         d = self.__make_dest_t(width, height, dest)
  174.         t = openvip_c.openvip_create_thumbnails_generator(self.clib,
  175.                      file, stream, width, height, frames, d)
  176.         if t == None:
  177.             raise Error("failed to load file '%s'" % file)
  178.         task = Task(self, t, dest)
  179.         task.width = width
  180.         task.height = height
  181.         return task
  182.  
  183.  
  184.     def enum_plugins(self, type):
  185.         pllist = openvip_c.openvip_enum_plugins(self.clib, type)
  186.         outlist = []
  187.         for i in range(0, pllist.cnt):
  188.             outlist.append(PluginInfo(
  189.                 openvip_c.openvip_plugin_info_t_array_getitem(
  190.                     pllist.plugins, i)))
  191.         openvip_c.openvip_free(self.clib, pllist)
  192.         return outlist
  193.     
  194.  
  195.     def get_file_info(self, filename):
  196.         fi_c = openvip_c.openvip_get_file_info(self.clib, filename)
  197.         if fi_c == None:
  198.             raise Error("cannot get information about file '%s'" % filename)
  199.         fi = FileInfo(fi_c)
  200.         openvip_c.openvip_free(self.clib, fi_c)
  201.         return fi
  202.  
  203.  
  204.     def get_task_file_info(self, task, module):
  205.         fi_c = openvip_c.openvip_get_task_file_info(self.clib,
  206.                                                     task.ctask, module)
  207.         if fi_c == None:
  208.             raise Error("cannot get information about network")
  209.         fi = FileInfo(fi_c)
  210.         openvip_c.openvip_free(self.clib, fi_c)
  211.         return fi
  212.  
  213.  
  214.     def set_ui_callback(self, callback):
  215.         # verify it is valid UICallback object (C code wouldn't work otherwise):
  216.         if not isinstance(callback, UICallback):
  217.             raise TypeError('callback object must derive from openvip.UICallback')
  218.         # remember callback object (this is needed because SetUICallback won't
  219.         # increase reference count!):
  220.         self.callback = callback
  221.         # finally, set the callback:
  222.         openvip_c.SetUICallback(self.clib, callback)
  223.  
  224.